package com.ninetaildemonfox.zdl.socialproject.utils.dialog;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.hyphenate.easeui.utils.Preferences;
import com.hyphenate.easeui.utils.map.JSONUtils;
import com.kongzue.baseokhttp.HttpRequest;
import com.kongzue.baseokhttp.listener.ResponseListener;
import com.kongzue.baseokhttp.util.Parameter;
import com.kongzue.dialog.interfaces.OnMenuItemClickListener;
import com.kongzue.dialog.v3.BottomMenu;
import com.kongzue.dialog.v3.WaitDialog;
import com.kongzue.takephoto.TakePhotoUtil;
import com.ninetaildemonfox.zdl.socialproject.base.BaseActivity;
import com.ninetaildemonfox.zdl.ninetaildemonfox.utils.AppManager;
import com.ninetaildemonfox.zdl.socialproject.R;
import com.ninetaildemonfox.zdl.socialproject.mvp.contract.SelectiveSexContract;
import com.ninetaildemonfox.zdl.socialproject.mvp.presenter.SelectiveSexPresenter;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity.DateDetailsActivity;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity.EditorialMaterialsActivity;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity.MainActivity;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity.PictureUploadActivity;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity.SelectiveSexActivity;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity.WelcomeActivity;
import com.ninetaildemonfox.zdl.socialproject.utils.GlideCacheUtil;
import com.ninetaildemonfox.zdl.socialproject.utils.ToastUtil;
import com.ninetaildemonfox.zdl.socialproject.utils.dialog.presenter.DialogP;

import java.io.File;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建人： Nine tails fox
 * 创建时间： 2019/7/17
 * 功能描述：弹出对话框
 * 联系方式：1037438704@qq.com
 *
 * @author NineTailDemonFox
 */
public class DialogShow extends BaseActivity implements SelectiveSexContract {
    @BindView(R.id.text_center)
    TextView textCenter;
    @BindView(R.id.text_parent)
    TextView textParent;
    @BindView(R.id.text_context)
    TextView textContext;
    private String count = "0";
    private DialogP dialogP;
    private String order_id;
    private int poation;
    private SelectiveSexPresenter selectiveSexPresenter;

    @Override
    protected int getLayoutResource() {
        return R.layout.dialog_layout_sex;
    }

    private GlideCacheUtil instance;
    private File file;

    @Override
    protected void onInitialization(Bundle bundle) {
        selectiveSexPresenter = new SelectiveSexPresenter(this, this);
        dialogP = new DialogP(this, me);
        count = getIntent().getExtras().getString("count");
        if (count.equals("3")) {
            textContext.setText("报名需要发你的正脸照片（只有她能看见）");
        } else if (count.equals("4")) {
            instance = GlideCacheUtil.getInstance();
            textContext.setText("确定要清除缓存吗？");
        } else if (count.equals("5")) {
            textContext.setText("确定要恢复已被用户焚毁的照片吗？");
        } else if (count.equals("6")) {
            order_id = getIntent().getExtras().getString("order_id");
            poation = getIntent().getExtras().getInt("poation");
            textContext.setText("报名需要发你的正脸照片（只有她能看见）");
        } else if (count.equals("7")) {
            textContext.setText("确定要删除该约会吗？");
        } else if (count.equals("8")) {
            textContext.setText("确定要取消全部收藏吗？");
        } else if (count.equals("9")) {
            textContext.setText("确定要结束报名吗？");
        }

        TakePhotoUtil.getInstance(this).setReturnPhoto(new TakePhotoUtil.ReturnPhoto() {
            @Override
            public void onGetPhotos(String[] selectImagePaths) {
                Log.d("TakePhotoUtil", "==============" + MainActivity.thisMainCount);
                if (MainActivity.thisMainCount == 0) {
                    WaitDialog.show(me, "");
                    file = new File(selectImagePaths[0]);
//                Logger.d(selectImagePaths[0]);
                    HttpRequest.POST(me, "OrderEnlist/addEnlist"
                            , new Parameter()
                                    .add("token", token)
                                    .add("order_id", order_id)
                                    .add("img", file)
                            , new ResponseListener() {
                                @Override
                                public void onResponse(String response, Exception error) {
                                    WaitDialog.dismiss();
                                    Map<String, String> map = JSONUtils.parseKeyAndValueToMap(response);
                                    if (map == null) {
                                        return;
                                    }
                                    ToastUtil.show(map.get("message"));
                                    Bundle bundle = new Bundle();
                                    Intent i = new Intent();
                                    bundle.putInt("poation", poation);
                                    i.putExtras(bundle);
                                    setResult(RESULT_OK, i);
                                    finish();
                                }
                            });
                } else {
                    //这里获取文件路径并上传
                    String selectImagePath = selectImagePaths[0];
                    //                             文件路径             1上传    2设置	              1视频                    2 图片
                    PictureUploadActivity.start(me, selectImagePath, "1", "2");
                }
            }

            @Override
            public void onError(Exception e) {
                e.printStackTrace();
            }
        });

    }

    @OnClick({R.id.text_center, R.id.text_parent})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.text_center:
                finish();
                break;
            case R.id.text_parent:
                if (count.equals("1")) {
                    selectiveSexPresenter.getEditSex(token, count + "");
                    //男孩
                } else if (count.equals("2")) {
                    selectiveSexPresenter.getEditSex(token, count + "");
                } else if (count.equals("3")) {

                } else if (count.equals("4")) {
                    instance.clearImageAllCache(me);
                    ShowToast(this, "清除成功");
                } else if (count.equals("6")) {
                    BottomMenu.show(me, new String[]{"拍照", "从相册里选择"}, new OnMenuItemClickListener() {
                        @Override
                        public void onClick(String text, int index) {
                            switch (index) {
                                case 0:
                                    //使用相机拍摄
                                    TakePhotoUtil.getInstance(me).doOpenCamera();
                                    break;
                                case 1:
                                    TakePhotoUtil.getInstance(me).doOpenGallery();
                                    break;
                                default:
                            }
                        }
                    });
                    return;
                } else if (count.equals("7")) {
                    AppManager.getInstance().killActivity(DateDetailsActivity.class);
                } else if (count.equals("8")) {
                    dialogP.cancelMylike(token);
                } else if (count.equals("9")) {
                    //结束报名
                    http();
                }
                finish();
                break;
            default:
        }
    }

    private void http() {
        HttpRequest.POST(me, "Order/endOrder"
                , new Parameter()
                        .add("token", token)
                        .add("order_id", order_id)
                , new ResponseListener() {
                    @Override
                    public void onResponse(String response, Exception error) {
                        if (error == null) {
                            Map<String, String> map = JSONUtils.parseKeyAndValueToMap(response);
                            Map<String, String> data = JSONUtils.parseKeyAndValueToMap(map.get("data"));
                            if (map.get("code").equals("1")) {
                                Bundle bundle = new Bundle();
                                Intent i = new Intent();
                                bundle.putInt("poation", poation);
                                i.putExtras(bundle);
                                setResult(RESULT_OK, i);
                            }
                            ToastUtil.show(map.get("message"));
                        }
                    }
                });
    }


    public static void start(Context context, String count) {
//        4 设置界面的缓存弹框  5恢复焚毁照片   6  报名的弹框  7删除约会 8 点击收藏  9是结束报名弹框
        Bundle bundle = new Bundle();
        bundle.putString("count", count);
        Intent intent = new Intent(context, DialogShow.class);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    public static void start(Context context, String count, String order_id, int poation) {
//        4 设置界面的缓存弹框  5恢复焚毁照片   6  结束报名的弹框  7删除约会 8 点击收藏  9是结束报名弹框
        Bundle bundle = new Bundle();
        bundle.putString("count", count);
        bundle.putString("order_id", order_id);
        bundle.putInt("poation", poation);
        Intent intent = new Intent(context, DialogShow.class);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    public static void start(AppCompatActivity context, String count, String order_id, int poation, int tag) {
//        4 设置界面的缓存弹框  5恢复焚毁照片   6  结束报名的弹框  7删除约会 8 点击收藏  9是结束报名弹框
        Bundle bundle = new Bundle();
        bundle.putString("count", count);
        bundle.putString("order_id", order_id);
        bundle.putInt("poation", poation);
        Intent intent = new Intent(context, DialogShow.class);
        intent.putExtras(bundle);
        context.startActivityForResult(intent, tag);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        TakePhotoUtil.getInstance(me).onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void loginSuccess(Map<String, String> data) {
        Preferences.getInstance().commit(me, "user", "sex", count);
        if (count.equals("1")) {
            startActivity(new Intent(DialogShow.this, WelcomeActivity.class));
            AppManager.getInstance().killActivity(SelectiveSexActivity.class);
        } else {
            //女孩
            EditorialMaterialsActivity.start(this, 1);
            AppManager.getInstance().killActivity(SelectiveSexActivity.class);
        }
    }

    @Override
    public void loginFailed(String msg) {

    }

    @Override
    public void showLoading() {

    }
}
